

class Node:
    def __init__(self):
        self.left = None
        self.right = None
        self.data = None

class Btree:
    def __init__(self, l=()):
        assert isinstance(l, (list, tuple))
        self.root = None
        self.count = 0
        if l:
            self.root = self.build(l, 0, len(l) - 1)

    def build(self, l, min, max):
        self.count += 1
        middle = int((min+max) / 2)
        node = Node()
        node.data = l[middle]
        if middle > min:
            node.left = self.build(l, min, middle - 1)
            print("Create left node: %d" % (node.left.data))
        if middle < max:
            node.right = self.build(l, middle + 1, max)
            print("Create right node: %d" % (node.right.data))

        return node
    def __len__(self):
        return self.count

    def _foreach(self, node):
        if node is None:
            return
        print(node.data)
        if node.left:
            self._foreach(node.left)
        if node.right:
            self._foreach(node.right)



    def loop(self):
        # self.()
        self._foreach(self.root)

if __name__ == '__main__':
    l =(2, 3, 4, 5, 6, 7, 8)
    b = Btree(l)
    b.loop()




















exit()




class FIFO:
    def __init__(self):
        self.data = None
        self.next = None
        self.prev = None


class Stack:
    def __init__(self):
        self.head = FIFO()
        self.tail = FIFO()
        self.length = 0

        self.head.next = self.tail
        self.tail.prev = self.head

    def is_empty(self):
        return self.length == 0

    def push(self, data):
        new_node = FIFO()
        new_node.data = data

        p_node = self.tail.prev
        p_node.next = new_node
        # new_node.next = self.tail

        self.tail.prev = new_node
        new_node.next = self.tail
        new_node.prev = p_node

        self.length += 1

    def pop(self):
        assert not self.is_empty()
        lastNode = self.tail.prev
        p_node = self.tail.prev.prev

        self.tail.prev = p_node
        p_node.next = self.tail
        self.length -= 1

        return lastNode.data

    def __iter__(self):
        node = self.head.next

        while node != self.tail:
            yield node.data
            node = node.next

    def __len__(self):
        return self.length




if __name__ == '__main__':


    l = Stack()
    for x in range(0, 10):
        l.push(x)

    for i in l:
        print(i)
    print('*' * 20)

    for i in range(0, len(l)):
        print(l.pop())








